Add --dry-run option to publish sub-command
authorWesley Moore <wes@wezm.net>
Sun, 17 Jul 2016 23:43:57 +0000 (09:43 +1000)
committerWesley Moore <wes@wezm.net>
Sun, 17 Jul 2016 23:43:57 +0000 (09:43 +1000)
Squashed commit of the following:

commit deed1d7b99c1cd142f7782d3b3b782d949e1f71f
Author: Wesley Moore <wes@wezm.net>
Date:   Fri Jul 15 13:35:01 2016 +1000

    Remove --dry-run and --no-verify mutual exclusion

commit 8a91fcf2a1aa3ba682fee67bb5b3e7c2c2cce8ef
Merge: 0c0d057 970535d
Author: Wesley Moore <wes@wezm.net>
Date:   Fri Jul 15 13:30:38 2016 +1000

    Merge remote-tracking branch 'upstream/master' into publish_dry_run

commit 0c0d0572533599b3c0e42797a6014edf480f1dc2
Author: Wesley Moore <wes@wezm.net>
Date:   Tue Jul 12 08:03:15 2016 +1000

    Improve grammar in --dry-run option

commit a17c1bf6f41f016cafdcb8cfc58ccbe34d54fbb8
Author: Wesley Moore <wes@wezm.net>
Date:   Mon Jul 11 14:17:41 2016 +1000

    Add test for passing no-verify and dry-run to publish

commit 284810cca5df3268596f18700c0247de2f621c98
Author: Wesley Moore <wes@wezm.net>
Date:   Mon Jul 11 14:51:38 2016 +1000

    Add test for publish --dry-run

commit 8514e47fbce61c20b227815887a377c25d17d004
Merge: 2b061c5 ef07b81
Author: Wesley Moore <wes@wezm.net>
Date:   Mon Jul 11 08:27:10 2016 +1000

    Merge branch 'publish_dry_run' of github.com:JustAPerson/cargo into publish_dry_run

commit ef07b81617df855328c34365b28049cd9742946c
Author: Jason Priest <jpriest128@gmail.com>
Date:   Tue Jun 9 23:11:51 2015 -0500

    Improve publish `--dry-run`

    Catch a few more errors by aborting midway through transmit().

commit 0686fb0bf92a09bcbd41e15e23ff03a0763c5d08
Author: Jason Priest <jpriest128@gmail.com>
Date:   Tue Jun 9 14:38:58 2015 -0500

    Teach publish the `--dry-run` flag

    Closes #1332

src/bin/publish.rs
src/cargo/ops/registry.rs
tests/publish.rs

index 97aa2e1376458e96f840a2909cfe3cc7b0739114..f5a9d82776722a63ed7d7ca57756ae1773a6fcea 100644 (file)
@@ -14,6 +14,7 @@ pub struct Options {
     flag_no_verify: bool,
     flag_allow_dirty: bool,
     flag_jobs: Option<u32>,
+    flag_dry_run: bool,
 }
 
 pub const USAGE: &'static str = "
@@ -30,6 +31,7 @@ Options:
     --allow-dirty            Allow publishing with a dirty source directory
     --manifest-path PATH     Path to the manifest of the package to publish
     -j N, --jobs N           Number of parallel jobs, defaults to # of CPUs
+    --dry-run                Perform all checks without uploading
     -v, --verbose ...        Use verbose output
     -q, --quiet              No output printed to stdout
     --color WHEN             Coloring: auto, always, never
@@ -47,6 +49,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
         flag_no_verify: no_verify,
         flag_allow_dirty: allow_dirty,
         flag_jobs: jobs,
+        flag_dry_run: dry_run,
         ..
     } = options;
 
@@ -59,6 +62,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
         verify: !no_verify,
         allow_dirty: allow_dirty,
         jobs: jobs,
+        dry_run: dry_run,
     }));
     Ok(None)
 }
index cda3d9abec9a6b1ea62c50d2ed0bd6d22ea13fe9..ac831b3576aa3878d126ce4ad5611ac159fdd50d 100644 (file)
@@ -36,6 +36,7 @@ pub struct PublishOpts<'cfg> {
     pub verify: bool,
     pub allow_dirty: bool,
     pub jobs: Option<u32>,
+    pub dry_run: bool,
 }
 
 pub fn publish(ws: &Workspace, opts: &PublishOpts) -> CargoResult<()> {
@@ -64,7 +65,7 @@ pub fn publish(ws: &Workspace, opts: &PublishOpts) -> CargoResult<()> {
 
     // Upload said tarball to the specified destination
     try!(opts.config.shell().status("Uploading", pkg.package_id().to_string()));
-    try!(transmit(&pkg, tarball.file(), &mut registry));
+    try!(transmit(opts.config, &pkg, tarball.file(), &mut registry, opts.dry_run));
 
     Ok(())
 }
@@ -87,8 +88,11 @@ fn verify_dependencies(pkg: &Package, registry_src: &SourceId)
     Ok(())
 }
 
-fn transmit(pkg: &Package, tarball: &File, registry: &mut Registry)
-            -> CargoResult<()> {
+fn transmit(config: &Config,
+            pkg: &Package,
+            tarball: &File,
+            registry: &mut Registry,
+            dry_run: bool) -> CargoResult<()> {
     let deps = pkg.dependencies().iter().map(|dep| {
         NewCrateDependency {
             optional: dep.is_optional(),
@@ -121,6 +125,13 @@ fn transmit(pkg: &Package, tarball: &File, registry: &mut Registry)
         }
         None => {}
     }
+
+    // Do not upload if performing a dry run
+    if dry_run {
+        try!(config.shell().warn("aborting upload due to dry run"));
+        return Ok(());
+    }
+
     registry.publish(&NewCrate {
         name: pkg.name().to_string(),
         vers: pkg.version().to_string(),
index b823672f93360498cf591bd7ace39f972f3cd845..63642a7bd4843eafae6eb96e9ef54e547dabb246 100644 (file)
@@ -327,3 +327,35 @@ fn new_crate_rejected() {
     assert_that(p.cargo("publish"),
                 execs().with_status(101));
 }
+
+#[test]
+fn dry_run() {
+    setup();
+
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+            license = "MIT"
+            description = "foo"
+        "#)
+        .file("src/main.rs", "fn main() {}");
+
+    assert_that(p.cargo_process("publish").arg("--dry-run"),
+                execs().with_status(0).with_stderr(&format!("\
+[UPDATING] registry `{reg}`
+[WARNING] manifest has no documentation, [..]
+[PACKAGING] foo v0.0.1 ({dir})
+[VERIFYING] foo v0.0.1 ({dir})
+[COMPILING] foo v0.0.1 [..]
+[UPLOADING] foo v0.0.1 ({dir})
+[WARNING] aborting upload due to dry run
+",
+        dir = p.url(),
+        reg = registry())));
+
+    // Ensure the API request wasn't actually made
+    assert!(!upload_path().join("api/v1/crates/new").exists());
+}